package com.topisv.tms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.topisv.tms.entity.Function;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.service.FunctionService;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 功能管理的Controller, 使用Restful风格的Urls:
 * 
 * List page     : GET /function/
 * Create page   : GET /function/create
 * Create action : POST /function/create
 * Update page   : GET /function/update/{id}
 * Update action : POST /function/update
 * Delete action : GET /function/delete/{id}
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/function")
public class FunctionController extends BaseDataTableController{

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		sortTypes.put("showname", "功能名称");
	}

	@Autowired
	private FunctionService functionService;

	@RequestMapping(value = "v2_show")
	public String list() {
		return "function/functionList";
	}
	
	@RequestMapping(value = "page",method = RequestMethod.POST)
	public void page(@RequestBody JSONParam[] paramJson, HttpServletRequest request,HttpServletResponse response){
		try {
			// 客户端参数
			QueryParamHolder paramHolder = collectParam(paramJson);
			//paramHolder.getSearchParams().put("EQ_status", "1");
			Page<Function> page = functionService.getFunction(paramHolder);
			// 封装查询结果
			response.getWriter().print(bulidJSON(page));
		} catch (Exception e) {
			
		}
	}
	
	@RequestMapping(value = "v2_create", method = RequestMethod.GET)
	public String createForm(Model model) {
		short restype = 0;
		List<Function> menu1 = functionService.getMenu(restype,"1");
		restype = 1;
		List<Function> menu2 = functionService.getMenu(restype,"1");
		menu1.addAll(menu2);
		model.addAttribute("menu1", menu1);
		model.addAttribute("function", new Function());
		model.addAttribute("action", "create");
		return "function/functionForm";
	}
	
	/*@RequestMapping(value = "getMenu", method = RequestMethod.GET)
	public String getMenu(Model model) {
		Long userid = getCurrentUserId();
		List<Function> list = functionService.getMenu(userid);
		model.addAttribute("functions",list);
		return "function/menu";
	}*/

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@RequestParam(value = "function_id", required = false) Long function_id,@Valid Function function, RedirectAttributes redirectAttributes) {
		Function func = new Function();
		if(null == function_id)
		{
			func.setId(null);
		}
		else
		{
			func.setId(function_id);
		}
		function.setFunction(func);
		functionService.saveFunction(function);
		redirectAttributes.addFlashAttribute("message", "创建功能成功");
		return "redirect:/function/v2_show";
	}

	@RequestMapping(value = "v2_update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		short restype = 0;
		List<Function> menu1 = functionService.getMenu(restype,"1");
		restype = 1;
		List<Function> menu2 = functionService.getMenu(restype,"1");
		menu1.addAll(menu2);
		model.addAttribute("menu1", menu1);
		model.addAttribute("function", functionService.getFunction(id));
		model.addAttribute("action", "update");
		return "function/functionForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@RequestParam(value = "function_id", required = false) Long function_id,@Valid @ModelAttribute("preloadBaseUsers") Function function, RedirectAttributes redirectAttributes) {
		Function func = new Function();
		if(null == function_id)
		{
			func.setId(null);
		}
		else
		{
			func.setId(function_id);
		}
		function.setFunction(func);
		functionService.saveFunction(function);
		redirectAttributes.addFlashAttribute("message", "更新功能成功");
		return "redirect:/function/v2_show";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		Function function = functionService.getFunction(id);
		function.setStatus("0");
		functionService.saveFunction(function);
		//functionService.deleteFunction(id);
		redirectAttributes.addFlashAttribute("message", "删除功能成功");
		return "redirect:/function/v2_show";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadFunction")
	public Function getRole(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return functionService.getFunction(id);
		}
		return null;
	}
}